package wang.conge.javasedemo.core.thread;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantTest {
	public static void main(String[] args) {
		Lock lock = new ReentrantLock();
		
		Model model = new Model();
		
		Runnable numCountThread = (() -> {
			lock.lock();
			try {
				model.addNum();
				System.out.println("num:"+ model.getNum());
				
				lock.lock();
				try{
					model.addFlg();
					System.out.println("flg:"+ model.getFlg());
				}finally{
					lock.unlock();
				}
				
			} finally {
				lock.unlock();
			}
		});
		
		for (int i = 1; i <= 10; i++) {
			new Thread(numCountThread).start();
		}
	}
	
	static class Model {
		private int num = 0;
		private int flg = 0;
		
		public int getNum() {
			return num;
		}

		public void addNum() {
			num = num + 1;
		}
		
		public int getFlg() {
			return flg;
		}
		
		public void addFlg() {
			flg = flg + 1;
		}
	}
}
